package org.nnxy.foodboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.nnxy.foodboot.entity.UserPreference;
import org.nnxy.foodboot.mapper.UserPreferenceMapper;
import org.nnxy.foodboot.service.UserPreferenceService;
import org.springframework.stereotype.Service;

/**
 * 用户偏好服务实现类
 */
@Service
@RequiredArgsConstructor
public class UserPreferenceServiceImpl extends ServiceImpl<UserPreferenceMapper, UserPreference> implements UserPreferenceService {

    private final UserPreferenceMapper userPreferenceMapper;

    @Override
    public UserPreference getUserPreferenceByUserId(Integer userId) {
        if (userId == null) {
            return null;
        }
        
        LambdaQueryWrapper<UserPreference> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPreference::getUserId, userId);
        
        return getOne(queryWrapper);
    }

    @Override
    public boolean saveOrUpdateUserPreference(UserPreference userPreference) {
        if (userPreference == null || userPreference.getUserId() == null) {
            return false;
        }
        
        // 查询是否已存在记录
        UserPreference existingPreference = getUserPreferenceByUserId(userPreference.getUserId());
        
        if (existingPreference != null) {
            // 更新现有记录
            userPreference.setId(existingPreference.getId());
            return updateById(userPreference);
        } else {
            // 新增记录
            return save(userPreference);
        }
    }

    @Override
    public boolean updateHealthNeeds(Integer userId, String healthNeeds) {
        if (userId == null) {
            return false;
        }
        
        UserPreference preference = getUserPreferenceByUserId(userId);
        
        if (preference != null) {
            // 更新现有记录
            LambdaUpdateWrapper<UserPreference> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(UserPreference::getId, preference.getId())
                    .set(UserPreference::getHealthNeeds, healthNeeds);
            
            return update(updateWrapper);
        } else {
            // 创建新记录
            UserPreference newPreference = new UserPreference();
            newPreference.setUserId(userId);
            newPreference.setHealthNeeds(healthNeeds);
            
            return save(newPreference);
        }
    }

    @Override
    public boolean updateTastePreference(Integer userId, String tastePreference) {
        if (userId == null) {
            return false;
        }
        
        UserPreference preference = getUserPreferenceByUserId(userId);
        
        if (preference != null) {
            // 更新现有记录
            LambdaUpdateWrapper<UserPreference> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(UserPreference::getId, preference.getId())
                    .set(UserPreference::getTastePreference, tastePreference);
            
            return update(updateWrapper);
        } else {
            // 创建新记录
            UserPreference newPreference = new UserPreference();
            newPreference.setUserId(userId);
            newPreference.setTastePreference(tastePreference);
            
            return save(newPreference);
        }
    }

    @Override
    public boolean updateIngredientPreference(Integer userId, String ingredientPreference) {
        if (userId == null) {
            return false;
        }
        
        UserPreference preference = getUserPreferenceByUserId(userId);
        
        if (preference != null) {
            // 更新现有记录
            LambdaUpdateWrapper<UserPreference> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(UserPreference::getId, preference.getId())
                    .set(UserPreference::getIngredientPreference, ingredientPreference);
            
            return update(updateWrapper);
        } else {
            // 创建新记录
            UserPreference newPreference = new UserPreference();
            newPreference.setUserId(userId);
            newPreference.setIngredientPreference(ingredientPreference);
            
            return save(newPreference);
        }
    }
} 